package inventoryservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type InventoryAssetReceiptPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetReceiptPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetReceiptPageLogic {
	return &InventoryAssetReceiptPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetReceiptPage 分页查询资产入库表
func (l *InventoryAssetReceiptPageLogic) InventoryAssetReceiptPage(in *property_service.InventoryAssetReceiptPageReq) (*property_service.InventoryAssetReceiptPageRes, error) {
	// 定义一个资产入库表模型对象切片用于存储查询结果
	var data []*model.InventoryAssetReceipt
	// 获取资产入库表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetReceipt
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID)) // 租户ID

	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理其他字段
		if in.Where.ReceiptCode != "" {
			do = do.Where(m.ReceiptCode.Like("%" + in.Where.ReceiptCode + "%")) // 入库单号
		}
		if in.Where.ReceiptDate != "" {
			do = do.Where(m.ReceiptDate.Eq(BeanUtils.GetDateFormat(in.Where.ReceiptDate))) // 入库日期
		}
		if in.Where.EquipmentSource != "" {
			do = do.Where(m.EquipmentSource.Like("%" + in.Where.EquipmentSource + "%")) // 设备来源
		}
		if in.Where.Receiver != "" {
			do = do.Where(m.Receiver.Like("%" + in.Where.Receiver + "%")) // 验收人
		}
		if in.Where.Warehouse != "" {
			do = do.Where(m.Warehouse.Like("%" + in.Where.Warehouse + "%")) // 库房
		}
		if in.Where.Supplier != "" {
			do = do.Where(m.Supplier.Like("%" + in.Where.Supplier + "%")) // 供应商
		}
		if in.Where.SupplierID > 0 {
			do = do.Where(m.SupplierID.Eq(in.Where.SupplierID)) // 仓库ID
		}
		if in.Where.PurchaseID > 0 {
			do = do.Where(m.PurchaseID.Eq(in.Where.PurchaseID)) // 采购申请单ID
		}
		if in.Where.PurchaseCode != "" {
			do = do.Where(m.PurchaseCode.Like("%" + in.Where.PurchaseCode + "%")) // 采购編碼
		}
		if in.Where.Purchaser != "" {
			do = do.Where(m.Purchaser.Like("%" + in.Where.Purchaser + "%")) // 采购员
		}
		if in.Where.ContractNumber != "" {
			do = do.Where(m.ContractNumber.Like("%" + in.Where.ContractNumber + "%")) // 合同编号
		}
		if in.Where.InvoiceNumber != "" {
			do = do.Where(m.InvoiceNumber.Like("%" + in.Where.InvoiceNumber + "%")) // 发票号码
		}
		if in.Where.InvoiceDate != "" {
			do = do.Where(m.InvoiceDate.Eq(BeanUtils.GetDateFormat(in.Where.InvoiceDate))) // 发票日期
		}
		if in.Where.DepreciationMethod != "" {
			do = do.Where(m.DepreciationMethod.Like("%" + in.Where.DepreciationMethod + "%")) // 折旧方法
		}
		if in.Where.DepreciationMethodCode != "" {
			do = do.Where(m.DepreciationMethodCode.Like("%" + in.Where.DepreciationMethodCode + "%")) // 折旧方法
		}
		if in.Where.AssetUse != "" {
			do = do.Where(m.AssetUse.Like("%" + in.Where.AssetUse + "%")) // 资产用途
		}
		if in.Where.ReceiptMethod != "" {
			do = do.Where(m.ReceiptMethod.Like("%" + in.Where.ReceiptMethod + "%")) // 入库方式
		}
		if in.Where.InstallationNumber != "" {
			do = do.Where(m.InstallationNumber.Like("%" + in.Where.InstallationNumber + "%")) // 安装单号
		}
		if in.Where.AssetUseCode != "" {
			do = do.Where(m.AssetUseCode.Like("%" + in.Where.AssetUseCode + "%")) // 资产用途Code
		}
		if in.Where.ReceiptMethodCode != "" {
			do = do.Where(m.ReceiptMethodCode.Like("%" + in.Where.ReceiptMethodCode + "%")) // 入库方式Code
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.InventoryAssetReceipt{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.InventoryAssetReceiptPageRes{
		Count: count,
		List:  list.([]*property_service.InventoryAssetReceipt),
	}, nil
}
